import React from 'react';
import loadjs from 'loadjs';
import { workflowPublicFiles } from '../constants/fileConfig';
import {observer} from "mobx-react/index";
import allStore from '../stores';

@observer
export default class Forward extends React.Component{
    constructor(){
        super();
        this.state = {hasLoad: false};
    }
    componentDidMount(){
        const init = () =>{
            const { WFForwardStore } = window.weaWorkflowPublic;
            const store = new WFForwardStore();
            this.setState({hasLoad: true,store:store});
        }
        loadjs.isDefined('workflowPublic') ? loadjs.ready('workflowPublic', init) : loadjs(workflowPublicFiles, 'workflowPublic', init);
    }

    onClose(){
    }

    //判断转发是否开启了拦截
    filterRequest4Forward(submitFuncParams, submitParam, authActions) {
        const REQUEST_FORWARD = "viewtype_forward";
        let filterResult = false;
        const authActionset = new Set([...authActions]);
        const {forwardflag} = submitFuncParams;
        if (forwardflag == "1") {     //转发
            if (authActionset.has(REQUEST_FORWARD)) {
                filterResult = true;
            }
        }
        return filterResult;
    }

    showAuthDialog(){
        const {wfListDoingStore} = this.props;
        //弹出验证或者数据保护的窗口
        wfListDoingStore.setShowSecondAuth(true);
    }

    render(){
        const { hasLoad ,store} = this.state;
        if(hasLoad){
            const {forwardParams = {}} = allStore.wfGlobalStore;
            const { WfForward} = window.weaWorkflowPublic;

            const {secondAuthStore} = this.props;
            const {workflowid, nodeid, requestid, f_weaver_belongto_userid, f_weaver_belongto_usertype} = forwardParams;
            const secondAuthParam = {
                workflowid,
                nodeid,
                requestid,
                f_weaver_belongto_userid,
                f_weaver_belongto_usertype
            };

            store.setShowForward(forwardParams,{});
            store.setSecondAuthParams(secondAuthParam, secondAuthStore, this.filterRequest4Forward.bind(this), this.onClose.bind(this), this.showAuthDialog.bind(this));
            return (<WfForward ecId={`${this && this.props && this.props.ecId || ''}_WfForward@ivhhlp`} wfForwardStore={store}/>);
        }else{
            return ( <div></div>);
        }
    }
}